package com.example.framework.base;

import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.app.Application;

/**
 * Custom Application will be used to set some constant instead of setting in
 * util_file and initialize CrashHandler which implements
 * UncaughtExceptionHandle.
 * 
 * @author renxin.sun
 * 
 */
public class CustomApplication extends Application
{
	private static CustomApplication application;

	public static final String ASYNC_TAG = "Async";

	public static final String IMAGECACHE_TAG = "RemoteImage";
	
	public static final String SQLITE_TAG = "Sqlite";

	/**
	 * Used as SharePreferences' key.Set it on Activity file.
	 */
	public static final String CACHE_SIZE = "cache_size";
	
	/**
	 *  Measured in the units of "MB" .
	 */
	public static float IMAGE_DEFAULT_CACHE = (float) 5;
	
	/**
	 *  Measured in the units of "MB" .
	 */
	public static float EXCEPTION_DEFAULT_CACHE = (float) 1;
	
	public static final String CACHE_DIR = "/xiaqianqian/";
	
	private ImageCache imageCache;
	
	private List<Activity> listActivities;

	@Override
	public void onCreate()
	{
		super.onCreate();

		application = this;
		
		imageCache = new ImageCache();

		listActivities = new ArrayList<Activity>();
		
		CrashHandler catchHandler = CrashHandler.getInstance();
		catchHandler.init(getApplicationContext());
	}

	/**
	 * Used to get CustomApplicaion instance.
	 * 
	 * @return Instance of CustomApplicaion.
	 */
	public static CustomApplication getInstance()
	{
		return application;
	}
	
	public ImageCache getImageCache()
	{
		return imageCache;
	}
	
	public void addActivity(Activity activity)
	{
		listActivities.add(activity);
	}
	
	/**
     * Causes the virtual machine to stop running and the program to exit. If
     * {@link #runFinalizersOnExit(boolean)} has been previously invoked with a
     * {@code true} argument, then all objects will be properly
     * garbage-collected and finalized first.
     *
     * @param code
     *            the return code.
     * @throws SecurityException
     *             if the running thread has not enough permission to exit the
     *             virtual machine.
     * @see SecurityManager#checkExit
     */
	public void exit(int code)
	{
		for (Activity activity : listActivities)
		{
			activity.finish();
		}
		System.exit(code);
	}
}
